package com.service.impl;

import com.dao.impl.PayImpl;
import com.dao.impl.VipImpl;
import com.service.PayService;
import com.entity.allPrices;
import com.entity.partPrice;

import java.util.List;

import static com.util.Vessel.*;

public class PaySerImpl implements PayService {

    //订单总价格
    @Override
    public double allPrice(int cId) {
        double price = 0;
        List<allPrices> allPricesList = pay.allPrice(cId);
        System.out.println("最后需支付总金额为:");
        for (allPrices prices : allPricesList){
//            System.out.println(String.format("%.2f",prices.getAllPrice()));
            System.out.println(prices.getAllPrice());
            price = prices.getAllPrice();
        }
        return price;
    }
    //单个订单总价
    @Override
    public double aPrice(int cId) {
        List<partPrice> priceList = pay.aPrice(cId);
        System.out.println("订单号\t\t\t" + "下单时间\t\t\t\t" + "订单金额");
        for(partPrice price : priceList){
            System.out.println(price.getoId() + "\t\t\t" + price.getoTime() + "\t\t\t" + price.getaPrice());
        }
        return 0;
    }
    //其他方式支付
    @Override
    public boolean weChat(int cId,double allPrices) {
        System.out.println("请输入支付金额(模拟)");
        double thePrice = scanner.nextDouble();
        if (thePrice == allPrices){
            int num = payImpl.weChat(cId);
            if (num > 0 ) System.out.println("支付成功");
        }
        return false;
    }

    //vip付款
    @Override
    public void vipPay(int cId,double allPrices,String inputPassword) {
        boolean isVip = vipImpl.vipJudge(cId);
        if (isVip){

            int num = payImpl.vipPay(cId,inputPassword,allPrices);
            if (num > 0){
                System.out.println("vip付款成功");
                vipSer.vipBalance(cId,inputPassword);
            }else {
                System.out.println("余额不足是否充值:1:前往充值     2:普通付款");
                int choice = scanner.nextInt();
                switch(choice){
                    case 1:
                        vipSer.chargeMoney(cId,inputPassword);
                        break;
                    case 2:
                        weChat(cId,allPrices);
                        break;
                }
            }
        }else {
            System.out.println("您还不是Vip，是否开通vip享九折优惠: 1:开通vip     2:退出");
            int choice = scanner.nextInt();
            switch(choice){
                case 1:
                    vipSer.joinVip(cId);
                    break;
                case 2:
                    PayWay(cId);
                    break;
            }
        }
    }

    //支付方式
    @Override
    public void PayWay(int cId) {
        double allPrices = 0;
        System.out.println("如下是待支付订单:");
        System.out.println("各单订总价格:");
        aPrice(cId);
        allPrices = allPrice(cId);
        System.out.println("请选择支付方式:   1:微信/支付宝/现金      2:会员卡支付(享9折)       3:开通会员卡");
        int choice = scanner.nextInt();
        switch(choice){
            case 1:
                weChat(cId,allPrices);
                break;
            case 2:
                System.out.println("请输入vip密码:");
                String inputPassword = scanner.next();
                System.out.print("当前vip");
                vipSer.vipBalance(cId,inputPassword);
                System.out.println("Vip用户需付款" + 0.9*allPrices + "元");
                vipPay(cId,allPrices,inputPassword);
                break;
            case 3:
                vipSer.joinVip(cId);
                break;
            case 4:
                break;
        }
    }

}
